﻿Public Class FrmMuonHoSoDichVu
    Dim dong As Integer
    Private Sub BatChucNang()
        txtNguoiMuon.Enabled = True
        dtNgayMuon.Enabled = True
        txtDonVi.Enabled = True
        cmbHinhThuc.Enabled = True
        txtMucDichMuon.Enabled = True
        txtTenCongTrinh.Enabled = True
        dgvDSCongTrinh.Enabled = True
        dgvDSNguoiDaMuon.Enabled = True
        btnLuu.Enabled = True
        btnInPhieu.Enabled = False
        '  LamTuoi()
        txtNguoiMuon.Focus()
    End Sub
    Private Sub TatChucNang()
        txtNguoiMuon.Enabled = False
        dtNgayMuon.Enabled = False
        txtDonVi.Enabled = False
        cmbHinhThuc.Enabled = False
        txtMucDichMuon.Enabled = False
        txtTenCongTrinh.Enabled = False

        dgvDSCongTrinh.Enabled = False
        dgvDSNguoiDaMuon.Enabled = False
        btnLuu.Enabled = False

    End Sub
    Private Sub LamTuoi()
        txtNguoiMuon.Text = ""
        txtDonVi.Text = ""
        txtMucDichMuon.Text = ""
        txtSoHoSo.Text = ""
        txtTenCongTrinh.Text = ""
        dtNgayNhan.Text = ""
        txtHoSoNam.Text = ""
        txtThanhPhanHoSo.Text = ""
    End Sub
    Private Function SoSanhTenCongTrinh() As Boolean
        Dim sql As String
        sql = "Select TenCongTrinh From CONGTRINH Where TenCongTrinh like N'" & txtTenCongTrinh.Text.Trim & "' AND SoHoSo = " & Int32.Parse(txtSoHoSo.Text) & ""
        Dim tb As DataTable = LoadThongTin(sql)
        If tb.Rows.Count > 0 Then
            Return True
        Else
            Return False
        End If
    End Function
    Private Sub NapDSCongTrinh()
        Dim sql As String
        sql = "SELECT CONGTRINH.SoHoSo, CONGTRINH.TenCongTrinh,   CONGTRINH.NgayNhan, CONVERT(CHAR(10), CONGTRINH.NgayNhan, 103) AS NgayNhan, CONGTRINH.HoSoNam, CONGTRINH.ThanhPhanHoSo, " &
              "TENHUYEN.T_Huyen, TENXA.TenXa, DMPhong.TenPhong, DMKe.TenKe, DMKho.TenKho, DMTinhTrang.TenTinhTrang " &
              "FROM CONGTRINH LEFT OUTER JOIN " &
              "DMPhong ON congtrinh.MaPhongID = DMPhong.MaPhongID LEFT OUTER JOIN " &
              "DMKe ON CONGTRINH.MaKeID = DMKe.MaKeID LEFT OUTER JOIN " &
              "DMKho ON CONGTRINH.MaKhoID = DMKho.maKhoID LEFT OUTER JOIN " &
              "DMTinhTrang ON CONGTRINH.MaTinhTrangID = DMTinhTrang.MaTinhTrangID LEFT OUTER JOIN " &
              "TENXA ON CONGTRINH.MaXa = TENXA.MaXa LEFT OUTER JOIN " &
              "TENHUYEN ON TENHUYEN.maH = TENXA.MaHuyen " &
              "WHERE CONGTRINH.MATINHTRANGID =1 " &
              "AND TENCONGTRINH  like N'%" & txtTenCongTrinh.Text.Trim & "%'"
        dgvDSCongTrinh.DataSource = LoadThongTin(sql)
        dgvDSCongTrinh.Refresh()
    End Sub
    Private Sub LoadDSNguoiDaMuonHS()
        Dim sql As String
        sql = "SELECT DISTINCT NGUOIMUON, DONVI FROM PHIEUMUON_HSDV WHERE NGUOIMUON like N'%" & txtNguoiMuon.Text.Trim & "%' "
        dgvDSNguoiDaMuon.DataSource = LoadThongTin(sql)
        dgvDSNguoiDaMuon.Refresh()
    End Sub

    Private Sub FrmMuonHoSoDichVu_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        LoadHuyen(cmbHuyen)
        LoadXa(Convert.ToInt32(cmbHuyen.SelectedValue.ToString), cmbXa)
        LoadPhong(cmbPhong)
        LoadKe(cmbKe)
        LoadKho(cmbKho)
        LoadHinhThuc(cmbHinhThuc)
        LoadTinhTrang(cmbTinhTrang)

        dtNgayMuon.Text = Date.Today()
        dtNgayNhan.Text = Date.Today()
        LoadDSNguoiDaMuonHS()
        NapDSCongTrinh()
        TatChucNang()
        btnNhap.Focus()
        btnInPhieu.Enabled = False

    End Sub

    Private Sub cmbHuyen_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbHuyen.SelectedIndexChanged
        LoadXa(Convert.ToInt32(cmbHuyen.SelectedValue.ToString), cmbXa)
    End Sub

    Private Sub btnNhap_Click(sender As Object, e As EventArgs) Handles btnNhap.Click
        'LamTuoi()
        BatChucNang()
        '  txtSoHoSo.Text = AutoID("CONGTRINH") + 1

        dtNgayMuon.Text = Date.Today()
    End Sub

    Private Sub btnLuu_Click(sender As Object, e As EventArgs) Handles btnLuu.Click
        Dim sql As String
        Dim tb As New DataTable

        'Kiem tra Ten Nguoi muon ko duoc rong
        If (txtNguoiMuon.Text.Trim = "") Then
            MessageBox.Show("Họ tên người mượn không được rỗng ")
            txtNguoiMuon.Focus()
            Return
        End If

        'Kiem tra Ngay muon ho so phai lon hon ngay nhan
        If (dtNgayMuon.Value < dtNgayNhan.Value) Then
            MessageBox.Show("Ngày mượn không hợp lệ ")
            dtNgayMuon.Focus()
            Return
        End If
        'Kiem tra Don vi khong duoc rong
        If (txtDonVi.Text.Trim = "") Then
            MessageBox.Show("Đơn vị không được rỗng ")
            txtDonVi.Focus()
            Return
        End If

        'Kiem tra Ten cong trinh khong duoc rong
        If (txtTenCongTrinh.Text.Trim = "") Then
            MessageBox.Show("Tên Công trình không được rỗng ")
            txtTenCongTrinh.Focus()
            Return
        End If

        'Kiem tra chua chon Ho so cong trinh de muon
        If (txtSoHoSo.Text.Trim = "") Then
            MessageBox.Show("Chưa chọn Hồ sơ công trình để mượn. Hoặc hồ sơ công trình không có ")

            Return
        End If

        If SoSanhTenCongTrinh() = False Then
            MessageBox.Show("Tên công trình không hợp lệ", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return
        End If

        Dim NgayMuon As String
        NgayMuon = String.Format("{0:MM/dd/yyyy}", dtNgayMuon.Value)

        sql = "INSERT INTO PHIEUMUON_HSDV " &
             "(NgayMuon, NguoiMuon, DonVi, SoHoSo, MucDichMuon, MaHinhThucID) " &
             "VALUES('" & NgayMuon & "', N'" & txtNguoiMuon.Text.Trim & "', " &
             "N'" & txtDonVi.Text & "', " & Int32.Parse(txtSoHoSo.Text) & ", N'" & txtMucDichMuon.Text & "', '" & cmbHinhThuc.SelectedValue.ToString & "' ) "

        Dim flag As Boolean
        flag = ThaoTacDuLieu(sql)

        If (flag = True) Then
            MessageBox.Show("Lưu thông tin phiếu mới thành công")
            CapNhatTinhTrang()
            LuuThongTinMuonHoSo()
            btnInPhieu.Enabled = True
            TatChucNang()

            'Loại bỏ hồ sơ đã mượn ra khỏi danh sách
            dgvDSCongTrinh.Rows.RemoveAt(dong)
        End If

    End Sub
    Private Sub CapNhatTinhTrang()
        Dim sql As String
        sql = "UPDATE CONGTRINH SET MATINHTRANGID = 2 WHERE SOHOSO = " & Int32.Parse(txtSoHoSo.Text) & ""
        Dim t As Boolean
        t = ThaoTacDuLieu(sql)
    End Sub
    Private Sub LuuThongTinMuonHoSo()
        Dim sql As String
        sql = "INSERT INTO QL_MuonHoSoDichVu (SoPhieuMuon,NguoiChoMuon) VALUES (" & AutoID("PHIEUMUON_HSDV") & ", '" & ModuleTenDangNhap.m_tennhap & "')"
        Dim t As Boolean
        t = ThaoTacDuLieu(sql)
    End Sub

    Private Sub btnThoat_Click(sender As Object, e As EventArgs) Handles btnThoat.Click
        Me.Close()
    End Sub

    Private Sub txtHoSoNam_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtHoSoNam.KeyPress

        If (Not Char.IsDigit(e.KeyChar) And Not Char.IsControl(e.KeyChar)) Then
            MessageBox.Show("Kí tự không được chấp nhận. Chỉ chấp nhận số")
            e.Handled = True
        End If
    End Sub
    Private Sub dgvDSCongTrinh_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvDSCongTrinh.CellClick

        dong = e.RowIndex

        'Kiem tra phai dong tieu de ko
        If (dong = -1) Then

            Return
        End If
        'Dua du lieu tu DataGridView len cac textbox va combox
        txtSoHoSo.Text = dgvDSCongTrinh.Rows(dong).Cells("SoHoSo").Value.ToString()
        txtTenCongTrinh.Text = dgvDSCongTrinh.Rows(dong).Cells("TenCongTrinh").Value.ToString()
        dtNgayNhan.Text = String.Format("{0:dd/MM/yyyy}", dgvDSCongTrinh.Rows(dong).Cells("NgayNhan").Value.ToString())
        txtHoSoNam.Text = dgvDSCongTrinh.Rows(dong).Cells("HoSoNam").Value.ToString()
        txtThanhPhanHoSo.Text = dgvDSCongTrinh.Rows(dong).Cells("ThanhPhanHoSo").Value.ToString()
        cmbHuyen.Text = dgvDSCongTrinh.Rows(dong).Cells("T_Huyen").Value.ToString()
        cmbXa.Text = dgvDSCongTrinh.Rows(dong).Cells("TenXa").Value.ToString()
        cmbPhong.Text = dgvDSCongTrinh.Rows(dong).Cells("TenPhong").Value.ToString()
        cmbKe.Text = dgvDSCongTrinh.Rows(dong).Cells("TenKe").Value.ToString()
        cmbKho.Text = dgvDSCongTrinh.Rows(dong).Cells("TenKho").Value.ToString()
        cmbTinhTrang.Text = dgvDSCongTrinh.Rows(dong).Cells("TenTinhTrang").Value.ToString()
    End Sub

    Private Sub txtTenCongTrinh_KeyUp(sender As Object, e As KeyEventArgs) Handles txtTenCongTrinh.KeyUp
        NapDSCongTrinh()
    End Sub

    Private Sub txtNguoiMuon_KeyUp(sender As Object, e As KeyEventArgs) Handles txtNguoiMuon.KeyUp
        LoadDSNguoiDaMuonHS()
    End Sub

    Private Sub dgvDSNguoiDaMuon_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvDSNguoiDaMuon.CellClick
        Dim dong As Integer
        dong = e.RowIndex

        'Kiem tra phai dong tieu de ko
        If (dong = -1) Then
            Return
        End If

        'Dua du lieu tu DataGridView len cac textbox va combox
        txtNguoiMuon.Text = dgvDSNguoiDaMuon.Rows(dong).Cells("NguoiMuon").Value.ToString()
        txtDonVi.Text = dgvDSNguoiDaMuon.Rows(dong).Cells("DonVi").Value.ToString()
    End Sub

    Private Sub btnInPhieu_Click(sender As Object, e As EventArgs) Handles btnInPhieu.Click

        Dim ts() As String = {"<SoPhieuMuon>", "<NguoiMuon>", "<DonVi>", "<TenCongTrinh>", "<SoHoSo>", "<ThuocHuyen>", "<ThuocXa>", "<TenKho>", "<TenKe>", "<TenPhong>", "<MucDichMuon>", "<HinhThuc>", "<NgayMuon>", "<NguoiNhap>"}
        Dim gt() As String = {AutoID("PHIEUMUON_HSDV"), txtNguoiMuon.Text, txtDonVi.Text, txtTenCongTrinh.Text, txtSoHoSo.Text, cmbHuyen.Text.Trim, cmbXa.Text.Trim, cmbKho.Text.Trim, cmbKe.Text.Trim, cmbPhong.Text.Trim, txtMucDichMuon.Text, cmbHinhThuc.Text.ToString, dtNgayMuon.Text.ToString, GetTenNguoiNhap(ModuleTenDangNhap.m_tennhap)}
        ExportWord(ts, gt, "PhieuMuonHoSoDichVu", AutoID("PHIEUMUON_HSDV"))
    End Sub

    Private Sub dgvDSCongTrinh_RowPostPaint(sender As Object, e As DataGridViewRowPostPaintEventArgs) Handles dgvDSCongTrinh.RowPostPaint
        ' xuat so thu tu tu dong cho datagridview
        Dim strRowNumber As String = (e.RowIndex + 1).ToString
        While (strRowNumber.Length < dgvDSCongTrinh.RowCount.ToString.Length)
            strRowNumber = "0" & strRowNumber
        End While
        Dim Size As SizeF = e.Graphics.MeasureString(strRowNumber, MyBase.Font)
        If dgvDSCongTrinh.RowHeadersWidth < Size.Width + 20 Then dgvDSCongTrinh.RowHeadersWidth = Size.Width + 20
        Dim b As Brush = SystemBrushes.ControlText
        e.Graphics.DrawString(strRowNumber, MyBase.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y +
                              ((e.RowBounds.Height - Size.Height) / 2))
    End Sub
    Private Sub dgvDSNguoiDaMuon_RowPostPaint(sender As Object, e As DataGridViewRowPostPaintEventArgs) Handles dgvDSNguoiDaMuon.RowPostPaint
        ' xuat so thu tu tu dong cho datagridview
        Dim strRowNumber As String = (e.RowIndex + 1).ToString
        While (strRowNumber.Length < dgvDSNguoiDaMuon.RowCount.ToString.Length)
            strRowNumber = "0" & strRowNumber
        End While
        Dim Size As SizeF = e.Graphics.MeasureString(strRowNumber, MyBase.Font)
        If dgvDSNguoiDaMuon.RowHeadersWidth < Size.Width + 20 Then dgvDSNguoiDaMuon.RowHeadersWidth = Size.Width + 20
        Dim b As Brush = SystemBrushes.ControlText
        e.Graphics.DrawString(strRowNumber, MyBase.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y +
                              ((e.RowBounds.Height - Size.Height) / 2))
    End Sub
End Class